package com.hjtm.easypigfarm.utility;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.hjtm.easypigfarm.R;


public class PigUtils {
    
    public final String TAG = PigUtils.class.getSimpleName();
    private Handler mHandler;
    private ArrayList<String> mProvinces= new ArrayList<String>();
    
    public PigUtils(Handler handle) {
        mHandler = handle;
        if (mHandler != null) {
            new NetAsyncTask().execute();
        }
    }
    
    public ArrayList<String> getProvinces() {
        return mProvinces;
    }
    
    // get Apk version
    public static String getVersion(Context context)
    {
        try {
            PackageInfo pi=context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            return pi.versionName;
        } catch (NameNotFoundException e) {
            e.printStackTrace();
            return context.getString(R.string.version_unknown);

        }
    }
    
    class NetAsyncTask extends AsyncTask<Object, Object, String> {

        @Override
        protected void onPostExecute(String result) {
            Log.i(TAG, "dayongww result:" + result);
            if (result.equals("success")) {
                if (mHandler != null) {
                    Message msg = mHandler.obtainMessage(Constants.MSG_UPDATE_FARMS, mProvinces);
                    mHandler.sendMessage(msg);
                }
            }
            super.onPostExecute(result);
        }

        @Override
        protected String doInBackground(Object... params) {
            return "success";
        }
    }

}
